package cn.tedu.clyback.role.service.impl;


import cn.tedu.clyback.RolePermission.mapper.RolePermissionMapper;
import cn.tedu.clyback.role.mapper.RoleMapper;
import cn.tedu.clyback.role.pojo.dto.InsertPermissionParam;
import cn.tedu.clyback.role.pojo.dto.RoleSaveParam;
import cn.tedu.clyback.role.pojo.dto.RoleSelectParam;
import cn.tedu.clyback.role.pojo.entity.Role;
import cn.tedu.clyback.role.pojo.entity.RolesPermissions;
import cn.tedu.clyback.role.pojo.vo.RoleVO;
import cn.tedu.clyback.role.pojo.vo.SelectUserByRoleIdVo;
import cn.tedu.clyback.role.service.RoleService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class RoleServiceImpl implements cn.tedu.clyback.role.service.RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Override
    public List<RoleVO> selectRole(RoleSelectParam roleSelectParam) {
        log.debug("开始处理【查询角色】的业务，参数：{}", roleSelectParam);
        List<RoleVO> list = roleMapper.selectRole(roleSelectParam);
        log.debug("查询角色成功，结果：{}", list);
        return list;
    }

    @Override
    public void saveRole(RoleSaveParam roleSaveParam) {
        log.debug("开始处理【保存角色】的业务，参数：{}", roleSaveParam);
        Role role = new Role();
        BeanUtils.copyProperties(roleSaveParam, role);
        if (role.getRoleId() == null){
            role.setCreatedAt(new Date());
            roleMapper.insert(role);
        }else {
            role.setUpdatedAt(new Date());
            roleMapper.update(role);
        }
    }

    @Override
    public void updateStatus(Integer roleId, String status) {
        log.debug("开始处理【修改角色状态】的业务，参数：{},{}", roleId, status);
        Role role = new Role();
        role.setRoleId(roleId);
        role.setStatus(status);
        roleMapper.updateStatus(role);
    }

    @Override
    public void deleteRole(Integer roleId) {
        log.debug("开始处理【删除角色】的业务，参数：{}", roleId);
        roleMapper.deleteRole(roleId);
    }

    @Override
    public List<SelectUserByRoleIdVo> selectUserByRoleId(Integer roleId) {
        List<SelectUserByRoleIdVo> list = roleMapper.selectUserByRoleId(roleId);
        return list;
    }

    @Override
    public void deleteUserRole( Integer roleId,Long userId) {
        roleMapper.deleteUserRole(roleId,userId);
    }

    @Override
    public List<SelectUserByRoleIdVo> selectUser(Integer roleId) {
        List<SelectUserByRoleIdVo> list = roleMapper.selectUser(roleId);
        return list;
    }

    @Override
    public void addUserRole(Integer roleId, Long userId) {
        roleMapper.addUserRole(roleId,userId);
    }

    @Override
    public List<RoleVO> selectUserRole(Long userId) {
        log.debug("开始处理【查询用户角色】的业务，参数：{}", userId);
        List<RoleVO> list = roleMapper.selectUserRole(userId);
        return list;
    }

    @Override
    public void removeUserRole(Integer roleId, Long userId) {
        roleMapper.removeUserRole(roleId,userId);
    }

    @Override
    public List<RoleVO> selectUnAddUserRole(Long userId) {
        List<RoleVO> list = roleMapper.selectUnAddUserRole(userId);
        return list;
    }


}